/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.domain;

/**
 *
 * @author Miguel
 */
public class PersonAuditor {

    private static final String NUMBERS_REG_EX = "[0-9]+";
    private static final String EMAIL_REGEX = "[A-Za-z_0-9]+\\@[A-Za-z]+\\.[A-Za-z]+";
    private static final String CELLPHONE_REGEX = "[0-9]{10}";
    private static final String TELEPHONE_REGEX = "[0-9]{7}";

    public boolean isDataValid(Person inPerson) {
        boolean validData = true;

        String firstName = inPerson.getFirstName();
        String lastName = inPerson.getLastName();
        validData = areNamesValid(firstName, lastName);

        if (validData) {
            ContactInformation contactInfo = inPerson.getContactInformation();
            validData = isContactInfoValid(contactInfo);
        }
        return validData;
    }

    private boolean areNamesValid(String inFirstname, String inLastname) {
        boolean namesAccepted = true;
        if (inFirstname.isEmpty() || inLastname.isEmpty()) {
            namesAccepted = false;
        } else {
            if (!inFirstname.matches(NUMBERS_REG_EX) || !inLastname.matches(NUMBERS_REG_EX)) {
                namesAccepted = false;
            }
        }
        return namesAccepted;
    }

    private boolean isContactInfoValid(ContactInformation inContactInfo) {
        boolean contactInfoAccepted = true;

        String emailAddress = inContactInfo.getEmailAddress();
        contactInfoAccepted = isEmailValid(emailAddress);

        if (contactInfoAccepted) {
            String cellphoneNumber = inContactInfo.getTelephoneNumber(ContactInformation.CELLPHONE_NUMBER);
            contactInfoAccepted = isCellphoneNumberValid(cellphoneNumber);
        }
        if (contactInfoAccepted) {
            String extraCellphoneNumber = inContactInfo.getTelephoneNumber(ContactInformation.ADDITIONAL_CELLPHONE_NUMBER);
            contactInfoAccepted = isCellphoneNumberValid(extraCellphoneNumber);
        }
        if (contactInfoAccepted) {
            String homePhoneNumber = inContactInfo.getTelephoneNumber(ContactInformation.HOME_PHONE_NUMBER);
            contactInfoAccepted = isTelephoneNumberValid(homePhoneNumber);
        }
        if (contactInfoAccepted) {
            String extraHomephoneNumber = inContactInfo.getTelephoneNumber(ContactInformation.ADDITIONAL_HOME_NUMBER);
            contactInfoAccepted = isTelephoneNumberValid(extraHomephoneNumber);
        }
        return contactInfoAccepted;
    }

    private boolean isEmailValid(String inEmailAddress) {
        boolean isMailValid = true;
        if (!inEmailAddress.matches(EMAIL_REGEX)) {
            isMailValid = false;
        }
        return isMailValid;
    }

    private boolean isCellphoneNumberValid(String inCellphoneNumber) {
        boolean validCellphoneNumber = true;
        if (!inCellphoneNumber.isEmpty()) {
            if (!inCellphoneNumber.matches(CELLPHONE_REGEX)) {
                validCellphoneNumber = false;
            }
        }
        return validCellphoneNumber;
    }

    private boolean isTelephoneNumberValid(String inTelephoneNumber) {
        boolean validTelephoneNumber = true;
        if (!inTelephoneNumber.isEmpty()) {
            if (!inTelephoneNumber.matches(TELEPHONE_REGEX)) {
                validTelephoneNumber = false;
            }
        }
        return validTelephoneNumber;
    }
}
